﻿using System;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace Firefly.Framework.Graphics
{
    public abstract class ShaderProxy : IShaderProxy
    {
        public ShaderProxy(IDrawable parent)
        {
            Parent = parent;
        }

        #region IShaderProxy

        public Game Game { get; protected set; }
        public IDrawable Parent { get; protected set; }
        public string EffectName { get; protected set; }
        public Effect Effect { get; protected set; }

        public abstract void Draw(GameTime gameTime);

        #endregion

        #region IHasViews Members

        public virtual Matrix View
        {
            get { return Effect.Parameters["View"].GetValueMatrix(); }
            set { Effect.Parameters["View"].SetValue(value); }
        }
        public virtual Matrix Projection
        {
            get { return Effect.Parameters["Projection"].GetValueMatrix(); }
            set { Effect.Parameters["Projection"].SetValue(value); }
        }
        public virtual Matrix World
        {
            get { return Effect.Parameters["World"].GetValueMatrix(); }
            set { Effect.Parameters["World"].SetValue(value); }
        }

        #endregion

        #region IHasContent Members

        public abstract void LoadContent();

        public abstract void UnloadContent();

        #endregion

        #region IDisposableEx Members

        public bool Disposed { get; protected set; }

        #endregion

        #region IDisposable Members

        public void Dispose()
        {
            Dispose(true);
            GC.SuppressFinalize(this);
        }

        protected virtual void Dispose(bool disposing)
        {
            if (!Disposed)
            {
                if (disposing)
                {
                    UnloadContent();
                }

                Disposed = true;
            }
        }

        #endregion
    }
}
